﻿using System.Text.Json;

namespace DMSkin.Socket
{
    public class SerHelper
    {
        public SerHelper()
        {
        }

        public static byte[] Serialize(object obj)
        {
            string json = JsonSerializer.Serialize(obj);
            return System.Text.Encoding.UTF8.GetBytes(json);
        }

        public static T Deserialize<T>(byte[] buffer)
        {
            string json = System.Text.Encoding.UTF8.GetString(buffer);
            return JsonSerializer.Deserialize<T>(json);
        }

        public static object Deserialize(byte[] datas, int index)
        {
            string json = System.Text.Encoding.UTF8.GetString(datas, index, datas.Length - index);
            return JsonSerializer.Deserialize(json, typeof(object));
        }
    }
}